#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
#include "RingQueue.hpp"
#include "Task.hpp"

#define c_num 4
#define p_num 4

void* c_routine(void* args)
{
    pthread_detach(pthread_self());
    RingQueue<Task>* rq=(RingQueue<Task>*)args;
    while(true)
    {
        Task task;
        rq->pop(&task);
        task(pthread_self());
    }
}

void* p_routine(void* args)
{
    pthread_detach(pthread_self());
    RingQueue<Task>* rq=(RingQueue<Task>*)args;
    while(true)
    {
        int x=rand()%100+1;
        usleep(5843);
        int y=rand()%100+1;
        Task task(x,y);
        logMessage(NORMAL,"[%d]生产:%d+%d=?",pthread_self(),x,y);
        rq->push(task);
        sleep(1);
    }
}

int main()
{
    srand((unsigned int)time(nullptr));
    pthread_t consume[c_num];
    pthread_t product[p_num];
    RingQueue<Task>* rq=new RingQueue<Task>();

    for(int i=0;i<c_num;i++)
    {
        pthread_create(consume+i,nullptr,c_routine,(void*)rq);
    }
    for(int i=0;i<p_num;i++)
    {
        pthread_create(product+i,nullptr,p_routine,(void*)rq);
    }

    while(true)
    {
        //not do
    }
    return 0;
}